package com.zdzh.socketio.controller;

import com.alibaba.fastjson.JSON;
import com.zdzh.socketio.dto.PushMsg;
import com.zdzh.socketio.socket.session.ClientSessionHolder;
import com.corundumstudio.socketio.SocketIOClient;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;
import java.util.UUID;

/**
 * @ author Shawn(Shawn_0108@163.com)
 * @ date & time 2023/10/12 14:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/send")
public class SendController {

    private final StringRedisTemplate stringRedisTemplate;

    @GetMapping("/sendMsg")
    public void sendMsg() {
        Optional<SocketIOClient> socketIOClient = ClientSessionHolder.select("1@1");
        socketIOClient.ifPresent(client -> {
            client.sendEvent("sendMsg", "给你发个消息, 收到请给我回复哦!", "我真的收到了你的消息");
        });
    }

    @GetMapping("/topic")
    public String topic() {
        PushMsg pushMsg1 = new PushMsg();
        pushMsg1.setUuid(UUID.randomUUID().toString());
        pushMsg1.setUserId(1L);
        stringRedisTemplate.convertAndSend("fishListenerAdapter", JSON.toJSONString(pushMsg1));

        PushMsg pushMsg2 = new PushMsg();
        pushMsg2.setUuid(UUID.randomUUID().toString());
        pushMsg2.setUserId(2L);
        stringRedisTemplate.convertAndSend("catListenerAdapter", JSON.toJSONString(pushMsg2));
        return "success";
    }
}
